package com.example.volunteeroa.JpaUtils;

import com.example.volunteeroa.entity.*;
import com.example.volunteeroa.repository.*;

import java.util.List;
import java.util.Optional;

public class DeleteUtils {
    public static void deleteVolunteer(OrganizationRepository ORepository, VolunteerRepository VRepository,Volunteer volunteer){
        Optional<Volunteer> byId = VRepository.findById(volunteer.getVolunteerID());
        if (byId.isPresent()){
            Volunteer volunteer1 = byId.get();
            Organization organization = ORepository.findById(1).get();
            volunteer1.setOrg(organization);
            VRepository.save(volunteer1);
        }
    }
    //将要删除的志愿者移动到编号为"1"的默认未分配组织中，删除=打回到未分配的状态
    public static void deleteOrganization(VenueRepository VERepository, OrganizationRepository ORepository, VolunteerRepository VRepository, Organization organization){
        Optional<Organization> byId = ORepository.findById(organization.getOrgID());
        if (byId.isPresent()){
            Organization organization1 = byId.get();
            List<Volunteer> volunteers = organization1.getVolunteers();
            for (int i = 0; i < volunteers.size(); i++) {
                deleteVolunteer(ORepository,VRepository,volunteers.get(i));
            }
            Venue venue = VERepository.findById(1).get();
            organization1.setVenue(venue);
            ORepository.save(organization1);
        }
    }
    //将要删除的组织移动到编号为"1"的默认未分配场馆中，删除=打回到未分配的状态
    public static void deleteVenue(VenueRepository VERepository, OrganizationRepository ORepository, VolunteerRepository VRepository, Venue venue){
        Optional<Venue> byId = VERepository.findById(venue.getVenueID());
        if (byId.isPresent()){
            Venue venue1 = byId.get();
            List<Organization> organizations = venue1.getOrganizations();
            for (int i = 0; i < organizations.size(); i++) {
                deleteOrganization(VERepository,ORepository,VRepository,organizations.get(i));
            }
            VERepository.deleteById(venue1.getVenueID());
        }
    }

    public static void deleteOrgAllocByOrgID(OrganizationRepository ORepository,OrganizationAllocationRepository organizationAllocationRepository,Organization organization){
        Optional<Organization> byId = ORepository.findById(organization.getOrgID());
        if(byId.isPresent()){
            Organization organization1 = byId.get();
            List<OrganizationAllocation> organizationAllocations = organization1.getOrganizationAllocations();
            for (int i = 0; i < organizationAllocations.size(); i++) {
                organizationAllocationRepository.deleteById(organizationAllocations.get(i).getOAID());
            }
        }
    }

    public static void deleteVenueAllocByVenueID(VenueRepository venueRepository,VenueAllocationRepository venueAllocationRepository, Venue venue){
        Optional<Venue> byId = venueRepository.findById(venue.getVenueID());
        if(byId.isPresent()){
            Venue venue1 = byId.get();
            List<VenueAllocation> venueAllocations = venue1.getVenueAllocations();
            for (int i = 0; i < venueAllocations.size(); i++) {
                venueAllocationRepository.deleteById(venueAllocations.get(i).getVenueAID());
            }
        }
    }

}
